package com.leetcode2;

public class Index393validUtf8 {
	
	public boolean validUtf8(int[] data) {
        int n = data.length;
        for(int i = 0;i < n;){
            if(data[i] >= 128 && data[i] < 192 || data[i] > 247)
                return false;
            int index = changeTobineary(data[i]).indexOf("0");
            int j = i; 
            while(++i < index + j){
                if(i >= n || data[i] < 128 || data[i] >= 192)
                    return false;
            }
        }
        return true;
    }

    public String changeTobineary(int num){
        String ans = "";
        int count = 8;
        while(num > 0 || count > 0){
            ans = num % 2 + ans;
            num = num / 2;
            count--;
        }
        return ans;
    }
}